/*
 * @(#)WorkflowQueryRecord.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.dao;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import oracle.sql.BLOB;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.DataSourceFactory;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.bean.WorkflowQueryRecord;
import com.dcivision.workflow.web.ListWorkflowQueryRecordForm;
import com.dcivision.workflow.web.MaintWorkflowQueryRecordForm;

/**
 * WorkflowQueryRecordDAObject.java
 *
 * This class is the data access bean for table "WORKFLOW_QUERY_RECORD".
 *
 * @author      dick.xie
 * @company     DCIVision Limited
 * @creation date   19/11/2004
 * @version     $Revision: 1.7.2.1 $
 */

public class WorkflowQueryRecordDAObject extends AbstractDAObject {
  
  public static final String REVISION = "$Revision: 1.7.2.1 $";
  
  public static final String TABLE_NAME = "WORKFLOW_QUERY_RECORD";
  
  public WorkflowQueryRecordDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }
  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("WORKFLOW_GROUP_ID");
    this.vecDBColumn.add("USER_RECORD_ID");
    this.vecDBColumn.add("QUERY_NAME");
    this.vecDBColumn.add("QUERY_TYPE");
    this.vecDBColumn.add("XML_QUERY");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }
  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setWorkflowGroupID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          return(tmpWorkflowQueryRecord);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        
        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        
        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }
        
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
        
        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();
        
        this.positionCursor(rs, startOffset, pageSize);
        
        while (rs.next() && rowLoopCnt<pageSize) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setWorkflowGroupID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          tmpWorkflowQueryRecord.setRecordCount(totalNumOfRecord);
          tmpWorkflowQueryRecord.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }
  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setWorkflowGroupID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }
  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowQueryRecord tmpWorkflowQueryRecord = (WorkflowQueryRecord)((WorkflowQueryRecord)obj).clone();
    
    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        if (DataSourceFactory.DB_ORACLE!=DataSourceFactory.getDatabaseType()) {
          sqlStat.append("INSERT ");
          sqlStat.append("INTO   WORKFLOW_QUERY_RECORD(ID, WORKFLOW_GROUP_ID, USER_RECORD_ID, QUERY_NAME, QUERY_TYPE, XML_QUERY, DESCRIPTION, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
          sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, nextID);
          setPrepareStatement(preStat, 2, tmpWorkflowQueryRecord.getWorkflowGroupID());
          setPrepareStatement(preStat, 3, tmpWorkflowQueryRecord.getUserRecordID());
          setPrepareStatement(preStat, 4, tmpWorkflowQueryRecord.getQueryName());
          setPrepareStatement(preStat, 5, tmpWorkflowQueryRecord.getQueryType());
          setPrepareStatement(preStat, 6, tmpWorkflowQueryRecord.getXmlQuery());
          setPrepareStatement(preStat, 7, tmpWorkflowQueryRecord.getQueryDescription());
          setPrepareStatement(preStat, 8, GlobalConstant.RECORD_STATUS_ACTIVE);
          setPrepareStatement(preStat, 9, new Integer(0));
          setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 11, currTime);
          setPrepareStatement(preStat, 12, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 13, currTime);
          preStat.executeUpdate();
        }else{
          log.debug("Begin special handling for oracle update blob");
          sqlStat.append("INSERT ");
          sqlStat.append("INTO   WORKFLOW_QUERY_RECORD(ID, WORKFLOW_GROUP_ID, USER_RECORD_ID, QUERY_NAME, QUERY_TYPE, XML_QUERY, DESCRIPTION, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
          sqlStat.append("VALUES (?, ?, ?, ?, ?, EMPTY_BLOB(), ?, ?, ?, ?, ?, ?, ?) ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, nextID);
          setPrepareStatement(preStat, 2, tmpWorkflowQueryRecord.getWorkflowGroupID());
          setPrepareStatement(preStat, 3, tmpWorkflowQueryRecord.getUserRecordID());
          setPrepareStatement(preStat, 4, tmpWorkflowQueryRecord.getQueryName());
          setPrepareStatement(preStat, 5, tmpWorkflowQueryRecord.getQueryType());
          setPrepareStatement(preStat, 6, tmpWorkflowQueryRecord.getQueryDescription());
          setPrepareStatement(preStat, 7, GlobalConstant.RECORD_STATUS_ACTIVE);
          setPrepareStatement(preStat, 8, new Integer(0));
          setPrepareStatement(preStat, 9, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 10, currTime);
          setPrepareStatement(preStat, 11, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 12, currTime);
          preStat.executeUpdate();
          preStat.close();
          
          ResultSet rs = null;
          BLOB blob = null;
          sqlStat = new StringBuffer();
          sqlStat.append("SELECT  XML_QUERY ");
          sqlStat.append("FROM  WORKFLOW_QUERY_RECORD ");
          sqlStat.append("WHERE ID = " + nextID);
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          rs = preStat.executeQuery();
          if (rs.next()) {
            blob = (BLOB)rs.getBlob("XML_QUERY");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            try {
              ObjectOutputStream out = new ObjectOutputStream(baos);
              out.writeObject(tmpWorkflowQueryRecord.getXmlQuery());
              out.flush();
            } catch (java.io.IOException ioe) {
              log.error("Cannot write object stream", ioe);
              new java.sql.SQLException("Cannot write object stream");
            }
            blob.putBytes(1, baos.toByteArray());
            
            sqlStat = new StringBuffer();
            sqlStat.append("UPDATE  WORKFLOW_QUERY_RECORD ");
            sqlStat.append("SET XML_QUERY = ? ");
            sqlStat.append("WHERE ID = ? ");
            PreparedStatement preStat2 = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            preStat2.setBlob(1, blob);
            preStat2.setInt(2, nextID.intValue());
            preStat2.executeUpdate();
            try { preStat2.close(); } catch (Exception ignore) {} finally {preStat2 = null; }
          }
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        }
        tmpWorkflowQueryRecord.setID(nextID);        
        tmpWorkflowQueryRecord.setCreatorID(sessionContainer.getUserRecordID());
        tmpWorkflowQueryRecord.setCreateDate(currTime);
        tmpWorkflowQueryRecord.setUpdaterID(sessionContainer.getUserRecordID());
        tmpWorkflowQueryRecord.setUpdateDate(currTime);
        tmpWorkflowQueryRecord.setUpdateCount(new Integer(0));
        tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
        tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
        return(tmpWorkflowQueryRecord);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowQueryRecord tmpWorkflowQueryRecord = (WorkflowQueryRecord)((WorkflowQueryRecord)obj).clone();
    
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        if (DataSourceFactory.DB_ORACLE!=DataSourceFactory.getDatabaseType()) {
          sqlStat.append("UPDATE WORKFLOW_QUERY_RECORD ");
          sqlStat.append("SET  WORKFLOW_GROUP_ID=?, USER_RECORD_ID=?, QUERY_NAME=?, QUERY_TYPE=?, XML_QUERY=?, DESCRIPTION=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpWorkflowQueryRecord.getWorkflowGroupID());
          setPrepareStatement(preStat, 2, tmpWorkflowQueryRecord.getUserRecordID());
          setPrepareStatement(preStat, 3, tmpWorkflowQueryRecord.getQueryName());
          setPrepareStatement(preStat, 4, tmpWorkflowQueryRecord.getQueryType());
          setPrepareStatement(preStat, 5, tmpWorkflowQueryRecord.getXmlQuery());
          setPrepareStatement(preStat, 6, tmpWorkflowQueryRecord.getQueryDescription());
          setPrepareStatement(preStat, 7, new Integer(tmpWorkflowQueryRecord.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 9, currTime);
          setPrepareStatement(preStat, 10, tmpWorkflowQueryRecord.getID());
          setPrepareStatement(preStat, 11, tmpWorkflowQueryRecord.getUpdateCount());
          updateCnt = preStat.executeUpdate();
        }else{
          log.debug("Begin special handling for oracle update blob");
          sqlStat.append("UPDATE WORKFLOW_QUERY_RECORD ");
          sqlStat.append("SET  WORKFLOW_GROUP_ID=? USER_RECORD_ID=?, QUERY_NAME=?, QUERY_TYPE=?, XML_QUERY=EMPTY_BLOB(), DESCRIPTION=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
          sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, tmpWorkflowQueryRecord.getWorkflowGroupID());
          setPrepareStatement(preStat, 2, tmpWorkflowQueryRecord.getUserRecordID());
          setPrepareStatement(preStat, 3, tmpWorkflowQueryRecord.getQueryName());
          setPrepareStatement(preStat, 4, tmpWorkflowQueryRecord.getQueryType());
          setPrepareStatement(preStat, 5, tmpWorkflowQueryRecord.getQueryDescription());
          setPrepareStatement(preStat, 6, new Integer(tmpWorkflowQueryRecord.getUpdateCount().intValue() + 1));
          setPrepareStatement(preStat, 7, sessionContainer.getUserRecordID());
          setPrepareStatement(preStat, 8, currTime);
          setPrepareStatement(preStat, 9, tmpWorkflowQueryRecord.getID());
          setPrepareStatement(preStat, 10, tmpWorkflowQueryRecord.getUpdateCount());
          updateCnt = preStat.executeUpdate();
          if(updateCnt>0){
            ResultSet rs = null;
            BLOB blob = null;
            sqlStat = new StringBuffer();
            sqlStat.append("SELECT  XML_QUERY ");
            sqlStat.append("FROM  WORKFLOW_QUERY_RECORD ");
            sqlStat.append("WHERE ID = " + tmpWorkflowQueryRecord.getID());
            preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = preStat.executeQuery();
            if (rs.next()) {
              blob = (BLOB)rs.getBlob("XML_QUERY");
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
              try {
                ObjectOutputStream out = new ObjectOutputStream(baos);
                out.writeObject(tmpWorkflowQueryRecord.getXmlQuery());
                out.flush();
                log.debug("Finished writing blob");
              } catch (java.io.IOException ioe) {
                log.error("Cannot write object stream", ioe);
                new java.sql.SQLException("Cannot write object stream");
              }
              blob.putBytes(1, baos.toByteArray());
              
              sqlStat = new StringBuffer();
              sqlStat.append("UPDATE  WORKFLOW_QUERY_RECORD ");
              sqlStat.append("SET XML_QUERY = ? ");
              sqlStat.append("WHERE ID = ? ");
              PreparedStatement preStat2 = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
              preStat2.setBlob(1, blob);
              preStat2.setInt(2, tmpWorkflowQueryRecord.getID().intValue());
              preStat2.executeUpdate();
              log.debug("Finished updating parameter_object");
              try { preStat2.close(); } catch (Exception ignore) {} finally {preStat2 = null; }
            }
            try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          }
        }
        
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpWorkflowQueryRecord.setUpdaterID(sessionContainer.getUserRecordID());
          tmpWorkflowQueryRecord.setUpdateDate(currTime);
          tmpWorkflowQueryRecord.setUpdateCount(new Integer(tmpWorkflowQueryRecord.getUpdateCount().intValue() + 1));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          return(tmpWorkflowQueryRecord);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    WorkflowQueryRecord tmpWorkflowQueryRecord = (WorkflowQueryRecord)((WorkflowQueryRecord)obj).clone();
    
    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpWorkflowQueryRecord.getID());
        setPrepareStatement(preStat, 2, tmpWorkflowQueryRecord.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpWorkflowQueryRecord);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    WorkflowQueryRecord tmpWorkflowQueryRecord = (WorkflowQueryRecord)this.oldValue;
    
    if (tmpWorkflowQueryRecord != null) {
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getWorkflowGroupID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUserRecordID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryName()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryType()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getXmlQuery()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryDescription()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdateDate()));
    }
    
    tmpWorkflowQueryRecord = (WorkflowQueryRecord)obj;
    if (tmpWorkflowQueryRecord != null) {
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getID()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getWorkflowGroupID()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUserRecordID()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryName()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryType()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getXmlQuery()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getQueryDescription()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpWorkflowQueryRecord.getUpdateDate()));
    }
    
    auditTrailBase(opMode, oldValues, newValues);
  }
  
  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  /**
   * Gets a list of saved monitoring query record for the given user
   * 
   * @param userRecordID The user record ID
   * @return List of WorkflowQueryRecord
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord
   * @see com.dcivision.user.bean.UserRecord
   */
  public synchronized List getListByUserRecordID(Integer userRecordID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.DESCRIPTION, A.XML_QUERY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( A.USER_RECORD_ID = ? OR A.QUERY_TYPE = ? ) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, userRecordID);
        this.setPrepareStatement(preStat, 3, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of saved monitoring query record for the given user or query
   * <pre>  
   * ((MaintWorkflowQueryRecordForm)searchForm).setUserRecordID(userRecordID);
   * </pre>
   * 
   * The query type can be specified by,
   * <pre>
   *   searchForm.setBasicSearchField("QUERY_TYPE");
   *   searchForm.setBasicSearchType("=");
   *   searchForm.setBasicSearchKeyword(WorkflowQueryRecord.QUERY_TYPE_DEFAULT);
   * </pre>
   * 
   * @param searchForm
   *          The abstract search form object
   * @return List of WorkflowQueryRecord
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_DEFAULT
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_PRIVATE
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_PUBLIC
   * @see com.dcivision.workflow.web.MaintWorkflowQueryRecordForm
   */
  public  synchronized List getListByUserRecordIDOrQueryType(AbstractSearchForm searchForm) throws ApplicationException {
    
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    MaintWorkflowQueryRecordForm workflowQueryRecordForm = (MaintWorkflowQueryRecordForm)searchForm;
    Integer userRecordID = new Integer(workflowQueryRecordForm.getUserRecordID());
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
//        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( ( A.USER_RECORD_ID = ?  AND  A.WORKFLOW_GROUP_ID = ? ) OR ( A.USER_RECORD_ID <> ? AND A.QUERY_TYPE = ? ) ) ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( A.WORKFLOW_GROUP_ID = ? OR A.WORKFLOW_GROUP_ID IS NULL ) ");
        sqlStat.append(" AND ( A.USER_RECORD_ID = ? OR (A.QUERY_TYPE = ? AND USER_RECORD_ID <> ? ) ) ");
        
        
        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append(" AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        
        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append(" ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }
        
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);        
        this.setPrepareStatement(preStatCnt, 2, workflowQueryRecordForm.getWorkflowGroupID());
        this.setPrepareStatement(preStatCnt, 3, userRecordID);
        this.setPrepareStatement(preStatCnt, 4, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStatCnt, 5, userRecordID);
        
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 6, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
        
        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowQueryRecordForm.getWorkflowGroupID());
        this.setPrepareStatement(preStat, 3, userRecordID);
        this.setPrepareStatement(preStat, 4, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStat, 5, userRecordID);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 6, searchKeyword);
        }
        rs = preStat.executeQuery();
        
        this.positionCursor(rs, startOffset, pageSize);
        
        while (rs.next() && rowLoopCnt<pageSize) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          tmpWorkflowQueryRecord.setRecordCount(totalNumOfRecord);
          tmpWorkflowQueryRecord.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }
  /**
   * Gets a list of saved monitoring query record for the given user and query type
   * 
   * @param userRecordID The user record ID
   * @param queryType The query type
   * @return List of WorkflowQueryRecord
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_DEFAULT
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_PRIVATE
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord#QUERY_TYPE_PUBLIC
   */
  public  synchronized List getListByUserRecordIDAndQueryType(Integer userRecordID,String queryType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.USER_RECORD_ID = ? AND A.QUERY_TYPE = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, userRecordID);
        this.setPrepareStatement(preStat, 3, queryType);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  /**
   * Gets a list of saved monitoring query record by the given query name
   * 
   * @param queryName The query name
   * @return List of matched query record
   * @throws ApplicationException
   * @see com.dcivision.workflow.bean.WorkflowQueryRecord
   */
  public synchronized List getListByQueryName(String queryName) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ?  AND A.QUERY_NAME = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, queryName);
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get the query record list by workflow group ID, the list include the query record of the workflow group id is null
   * @param workflowGroupID
   * @return
   * @throws ApplicationException
   */
  public synchronized List getListByWorkflowGroupID(Integer workflowGroupID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.DESCRIPTION, A.XML_QUERY, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( A.WORKFLOW_GROUP_ID = ? OR A.WORKFLOW_GROUP_ID IS NULL ) ");
        sqlStat.append(" AND ( A.USER_RECORD_ID = ? OR (A.QUERY_TYPE = ? AND USER_RECORD_ID <> ? ) ) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, workflowGroupID);
        this.setPrepareStatement(preStat, 3, this.sessionContainer.getUserRecordID());
        this.setPrepareStatement(preStat, 4, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStat, 5, this.sessionContainer.getUserRecordID());
        rs = preStat.executeQuery();
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * get all accessible workflowQueryRecord List of current user.
   * @param searchForm ListWorkflowQueryRecordForm.
   * @return list.
   * @throws ApplicationException ApplicationException.
   */
  public  synchronized List getAllAccessibleList(AbstractSearchForm searchForm)
  throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    Integer userRecordID = sessionContainer.getUserRecordID();
    List accessibleGroupList = new ArrayList();
    ListWorkflowQueryRecordForm workflowQueryRecordForm = (ListWorkflowQueryRecordForm) searchForm;
    if (!Utility.isEmpty(workflowQueryRecordForm.getWorkflowGroupID())) {
        WorkflowGroup workflowGroup = new WorkflowGroup();
        workflowGroup.setID(new Integer(workflowQueryRecordForm.getWorkflowGroupID()));
        accessibleGroupList.add(workflowGroup);
    } else {
        WorkflowGroupDAObject workflowGroupDAO = new WorkflowGroupDAObject(sessionContainer, dbConn);
        accessibleGroupList = workflowGroupDAO.getAccessibleMonitoringList();
    }
    if (Utility.isEmpty(accessibleGroupList)) {
      return result;
    }
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( A.WORKFLOW_GROUP_ID IN (");
        for (int i = 0; i < accessibleGroupList.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(" ) OR A.WORKFLOW_GROUP_ID IS NULL ) ");
        sqlStat.append(" AND ( A.USER_RECORD_ID = ? OR (A.QUERY_TYPE = ? AND USER_RECORD_ID <> ? ) ) ");
        
        
        // Form the WHERE clause for filtering.
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append(" AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        
        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());
        
        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append(" ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }
        
        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        int counter = 2;
        for (int i=0; i < accessibleGroupList.size(); i++) {
          WorkflowGroup workflowGroup = (WorkflowGroup)
               accessibleGroupList.get(i);
          this.setPrepareStatement(preStatCnt, counter++, workflowGroup.getID());
        }
        this.setPrepareStatement(preStatCnt, counter++, userRecordID);
        this.setPrepareStatement(preStatCnt, counter++, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStatCnt, counter++, userRecordID);
        
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 6, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
        
        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        counter = 2;
        for (int i=0; i < accessibleGroupList.size(); i++) {
          WorkflowGroup workflowGroup = (WorkflowGroup)
               accessibleGroupList.get(i);
          this.setPrepareStatement(preStat, counter++, workflowGroup.getID());
        }
        this.setPrepareStatement(preStat, counter++, userRecordID);
        this.setPrepareStatement(preStat, counter++, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStat, counter++, userRecordID);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 6, searchKeyword);
        }
        rs = preStat.executeQuery();
        
        this.positionCursor(rs, startOffset, pageSize);
        
        while (rs.next() && rowLoopCnt<pageSize) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          tmpWorkflowQueryRecord.setRecordCount(totalNumOfRecord);
          tmpWorkflowQueryRecord.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  /**
   * get all accessible workflowQueryRecord List of current user.
   * @return list.
   * @throws ApplicationException ApplicationException.
   */
  public  synchronized List getAllAccessibleList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    int rowLoopCnt = 0;
    Integer userRecordID = sessionContainer.getUserRecordID();
    List accessibleGroupList = new ArrayList();
    WorkflowGroupDAObject workflowGroupDAO = new WorkflowGroupDAObject(sessionContainer, dbConn);
    accessibleGroupList = workflowGroupDAO.getAccessibleMonitoringList();
    if (Utility.isEmpty(accessibleGroupList)) {
      return result;
    }
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.WORKFLOW_GROUP_ID, A.USER_RECORD_ID, A.QUERY_NAME, A.QUERY_TYPE, A.XML_QUERY, A.DESCRIPTION, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   WORKFLOW_QUERY_RECORD A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND ( A.WORKFLOW_GROUP_ID IN (");
        for (int i = 0; i < accessibleGroupList.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(" ) OR A.WORKFLOW_GROUP_ID IS NULL ) ");
        sqlStat.append(" AND ( A.USER_RECORD_ID = ? OR (A.QUERY_TYPE = ? AND USER_RECORD_ID <> ? ) ) ");
        
        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Retrieve the result in row basis.
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        int counter = 2;
        for (int i=0; i < accessibleGroupList.size(); i++) {
          WorkflowGroup workflowGroup = (WorkflowGroup)
               accessibleGroupList.get(i);
          this.setPrepareStatement(preStat, counter++, workflowGroup.getID());
        }
        this.setPrepareStatement(preStat, counter++, userRecordID);
        this.setPrepareStatement(preStat, counter++, WorkflowQueryRecord.QUERY_TYPE_PUBLIC);
        this.setPrepareStatement(preStat, counter++, userRecordID);
        rs = preStat.executeQuery();
        
        while (rs.next()) {
          WorkflowQueryRecord tmpWorkflowQueryRecord = new WorkflowQueryRecord();
          tmpWorkflowQueryRecord.setID(getResultSetInteger(rs, "ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "WORKFLOW_GROUP_ID"));
          tmpWorkflowQueryRecord.setUserRecordID(getResultSetInteger(rs, "USER_RECORD_ID"));
          tmpWorkflowQueryRecord.setQueryName(getResultSetString(rs, "QUERY_NAME"));
          tmpWorkflowQueryRecord.setQueryType(getResultSetString(rs, "QUERY_TYPE"));
          tmpWorkflowQueryRecord.setXmlQuery(getResultSetObject(rs, "XML_QUERY"));
          tmpWorkflowQueryRecord.setQueryDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpWorkflowQueryRecord.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpWorkflowQueryRecord.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpWorkflowQueryRecord.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpWorkflowQueryRecord.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpWorkflowQueryRecord.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpWorkflowQueryRecord.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpWorkflowQueryRecord.setCreatorName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getCreatorID()));
          tmpWorkflowQueryRecord.setUpdaterName(UserInfoFactory.getUserFullName(tmpWorkflowQueryRecord.getUpdaterID()));
          ++rowLoopCnt;
          result.add(tmpWorkflowQueryRecord);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
}
